package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.LqDeviceInfo;
import com.ruoyi.report_lq.domain.LqzrdjlbBaDetail;
import com.ruoyi.report_lq.domain.bo.LqzrdjlbBaDetailBo;
import com.ruoyi.report_lq.domain.vo.LqDeviceVo;
import com.ruoyi.report_lq.domain.vo.LqzrdjlbBaDetailVo;
import com.ruoyi.report_lq.mapper.LqzrdjlbBaDetailMapper;
import com.ruoyi.report_lq.service.ILqzrdjlbBaDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青针入度记录子Service业务层处理
 *
 * @author zyb
 * @date 2024-06-11
 */
@RequiredArgsConstructor
@Service
public class LqzrdjlbBaDetailServiceImpl implements ILqzrdjlbBaDetailService {

    private final LqzrdjlbBaDetailMapper baseMapper;

    /**
     * 查询沥青针入度记录子
     */
    @Override
    public LqzrdjlbBaDetailVo queryById(Long lqzrdid){
        return baseMapper.selectVoById(lqzrdid);
    }

    /**
     *
     * @param infoId
     * @return 针入度集合
     * @method 根据info-id 查询针入度子表的list集合数据
     */
    public List<LqzrdjlbBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<LqzrdjlbBaDetail> query = new QueryWrapper<>();
        query.eq("lqzrdjlbid", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询沥青针入度记录子列表
     */
    @Override
    public TableDataInfo<LqzrdjlbBaDetailVo> queryPageList(LqzrdjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LqzrdjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<LqzrdjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青针入度记录子列表
     */
    @Override
    public List<LqzrdjlbBaDetailVo> queryList(LqzrdjlbBaDetailBo bo) {
        LambdaQueryWrapper<LqzrdjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LqzrdjlbBaDetail> buildQueryWrapper(LqzrdjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LqzrdjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLqzrdjlbid() != null, LqzrdjlbBaDetail::getLqzrdjlbid, bo.getLqzrdjlbid());
        lqw.eq(StringUtils.isNotBlank(bo.getShiybh()), LqzrdjlbBaDetail::getShiybh, bo.getShiybh());
        lqw.eq(bo.getShiywd() != null, LqzrdjlbBaDetail::getShiywd, bo.getShiywd());
        lqw.eq(bo.getDiyccz() != null, LqzrdjlbBaDetail::getDiyccz, bo.getDiyccz());
        lqw.eq(bo.getDieccz() != null, LqzrdjlbBaDetail::getDieccz, bo.getDieccz());
        lqw.eq(bo.getDisccz() != null, LqzrdjlbBaDetail::getDisccz, bo.getDisccz());
        lqw.eq(bo.getZhenrd() != null, LqzrdjlbBaDetail::getZhenrd, bo.getZhenrd());
        lqw.eq(bo.getZhenrdzs() != null, LqzrdjlbBaDetail::getZhenrdzs, bo.getZhenrdzs());
        return lqw;
    }

    /**
     * 新增沥青针入度记录子
     */
    @Override
    public Boolean insertByBo(LqzrdjlbBaDetailBo bo) {
        LqzrdjlbBaDetail add = BeanUtil.toBean(bo, LqzrdjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }
    /**
     * 批量新增沥青针入度记录子
     */
    public Boolean insertOrUpdateByZrdBo(List<LqzrdjlbBaDetailVo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            LqzrdjlbBaDetail detail = new LqzrdjlbBaDetail();
            detail.setExcelId(detailVo.getExcelId());
            detail.setDieccz(detailVo.getDieccz());
            detail.setDiyccz(detailVo.getDiyccz());
            detail.setDisccz(detailVo.getDisccz());
            detail.setShiybh(detailVo.getShiybh());
            detail.setShiywd(detailVo.getShiywd());
            detail.setLqzrdjlbid(pId);
            detail.setZhenrd(detailVo.getZhenrd());
            detail.setZhenrdzs(detailVo.getZhenrdzs());
            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }
    /**
     * 修改沥青针入度记录子
     */
    @Override
    public Boolean updateByBo(LqzrdjlbBaDetailBo bo) {
        LqzrdjlbBaDetail update = BeanUtil.toBean(bo, LqzrdjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LqzrdjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青针入度记录子
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
